/**
 * Created by mark on 2018/4/9.
 * 用户注册的页面逻辑
 */

require("./user-register.css");
require("page/common/nav-simple/nav-simple.js");
var _code = require("util/verification-code/verification-code.js");
var _user = require("service/user-service.js");
var _verifyCode = require("service/verifyCode-service.js")
var _mm = require("util/mm.js");
var  formError = {
    show : function(errMsg){
        $(".error-item").show().find(".err-msg").text(errMsg);
    },
    hide : function(){
        $(".error-item").hide().find(".err-msg").text("");
    }
}

var page = {
    data : {
        verifyCode : true //todo 标记验证码是否通过
    },
    init : function(){
        _code.loadCode();
        this.bindEvent();
    },
    bindEvent : function(){
        var _this = this;
        //1.验证码验证
        $("#verification-code-value").blur(function(){
            var codeValue = $.trim($(this).val());
            if(!codeValue){
                formError.show("验证码不能为空！");
                return;
            }
            _verifyCode.checkCode(codeValue,
                function (res){
                    _this.data.verifyCode = true;
                    formError.hide();
                    _code.changeCode();
                }, function (err) {
                    _code.changeCode();
                    formError.show(err.msg);
                })
        });

        //2.验证用户名是否已经存在
        $("#username").blur(function(){
            var username = $.trim($(this).val());
            //如果没有输入，username为空时则无需做验证
            if(!username){
                return;
            }
            _user.checkUsername(username, function(){
                    formError.hide();
                },function(err){
                    if(err.status === 1){
                        formError.show(err.msg);
                    }else{
                        formError.show("服务器异常！请联系管理员");
                    }
                }
            );
        });

        //3.验证用户名是否已经存在
        $("#email").blur(function(){
            var email = $.trim($(this).val());
            //如果没有输入，username为空时则无需做验证
            if(!email){
                return;
            }
            _user.checkEmail(email, function(){
                    formError.hide();
                },function(err){
                    if(err.status === 1){
                        formError.show(err.msg);
                    }else{
                        formError.show("服务器异常！请联系管理员");
                    }
                }
            );
        });

        //4.点击提交
        $("#submit").click(function(){
            if(_this.data.verifyCode){
                _this.submit();
            }else{
                formError.show("验证码校验不通过");
            }

        });

        $(".user-content").keyup(function(e){ //5.键盘输入回车键也会触发提交表单事件
            if(e.keyCode === 13){
                _this.submit();
            }
        });
    },

    //提交表单
    submit : function(){
        var fromData = {
            username : $.trim($("#username").val()),
            password : $.trim($("#password").val()),
            passwordConfirm : $.trim($("#password-confirm").val()),
            phone : $.trim($("#phone").val()),
            email : $.trim($("#email").val()),
            question : $.trim($("#question").val()),
            answer : $.trim($("#answer").val())
        };
        var validateResult = this.formValidate(fromData);
        if(validateResult.status){//验证通过
            _user.register(//异步注册
                fromData//请求的data
                ,
                function(){//请求成功的回调函数
                    window.location.href = "./result.html?type=register";
                }
                ,
                function(err){//请求失败的回调函数
                    if(err.status === 1){
                        formError.show(err.msg); //显示错误信息
                    }else{
                        console.error("-------注册时的异步请求失败： "+ err.msg) ;
                        formError.show("服务器异常！请稍后再试"); //显示错误信息
                    }
                }
            );
        }else{//验证失败
            //错误提示
            formError.show(validateResult.msg);
        }
    },
    //表单字段验证
    formValidate : function(formData){
        var result = {
            status : false,
            msg : ""
        };
        //验证用户名不能为空
        if(!_mm.validate(formData.username,"require")){
            result.msg = "用户名不能为空";
            return result;
        }
        //验证密码不能为空
        if(!_mm.validate(formData.password,"require")){
            result.msg = "密码不能为空！";
            return result;
        }
        //验证密码长度不少于6位数
        if(formData.password.length < 6){
            result.msg = "密码的长度不能少于6位！";
            return result;
        }
        //验证两次的输入密码是否一致
        if(formData.password !== formData.passwordConfirm){
            result.msg = "两次输入的密码不相同！";
            return result;
        }
        //验证手机号的格式
        if(!_mm.validate(formData.phone,"phone")){
            result.msg = "输入的手机号格式不正确！";
            return result;
        }
        //验证email格式
        if(!_mm.validate(formData.email,"email")){
            result.msg = "输入的邮箱格式不正确";
            return result;
        }
        //验证找回密码的提示问题不为空
        if(!_mm.validate(formData.question,"require")){
            result.msg = "问题不能为空！";
            return result;
        }
        //验证问题的答案不能为空
        if(!_mm.validate(formData.answer,"require")){
            result.msg = "回答问题的答案不能为空！"
            return result;
        }
        //通过验证返回，正确提示
        result.status = true;
        result.msg = "验证通过";
        return result;
    }

};
$(function(){
    page.init();
});